<template>
	<view>
		<view class="flex">
			<image @click="onChooseAvatar" class="avatar-wrapper" :src="userInfo.avatarUrl?userInfo.avatarUrl:userAUrl"
				mode="aspectFit"></image>
			<uni-icons color="#aaa" v-show="showPrivacy" type="eye-filled" size="30" @click="showPrivacy = false">
			</uni-icons>
			<uni-icons color="#aaa" v-show="!showPrivacy" type="eye-slash-filled" size="30" @click="showPrivacy = true">
			</uni-icons>
		</view>
		<uni-list v-if="!realPass&&type!=1" style="margin-top: 10px;">
			<uni-list-item showArrow link :to="'/pages/UpdateInfo/updatePage?title=姓名&type=text&value='+userInfo.name"
				:rightText="namePass(userInfo.name)" title="姓名" />
			<uni-list-item showArrow title="手机号" link
				:to="'/pages/UpdateInfo/updatePage?title=手机号&type=number&value='+userInfo.phone"
				:rightText="phonePass(userInfo.phone)" />
			<uni-list-item showArrow title="身份证号"
				:to="'/pages/UpdateInfo/updatePage?title=身份证号&type=password&value='+userInfo.idCard"
				:rightText="idCardPass(userInfo.idCard)" />
			<uni-list-item showArrow title="签收授权码" link
				:to="'/pages/UpdateInfo/updatePage?title=签收授权码&type=number&value='+userInfo.smscode"
				:rightText="userInfo.smscode" />
			<uni-list-item showArrow title="认证日期" :rightText="userInfo.createTime" />
			<uni-list-item showArrow title="签名设置" clickable :rightText="setSign" @click="toSignPage" />
		</uni-list>
		<uni-list v-else style="margin-top: 10px;">
			<uni-list-item showArrow :rightText="namePass(userInfo.name)" title="姓名" clickable @click="showToast" />
			<uni-list-item showArrow title="手机号" link
				:to="'/pages/UpdateInfo/updatePage?title=手机号&type=number&value='+userInfo.phone"
				:rightText="phonePass(userInfo.phone)" />
			<uni-list-item showArrow title="身份证号" :rightText="idCardPass(userInfo.idCard)" clickable
				@click="showToast" />
			<uni-list-item showArrow title="签收授权码" link
				:to="'/pages/UpdateInfo/updatePage?title=签收授权码&type=number&value='+userInfo.smscode"
				:rightText="userInfo.smscode" />
			<uni-list-item showArrow title="认证日期" :rightText="userInfo.createTime" />
			<uni-list-item showArrow title="签名设置" clickable :rightText="setSign" @click="toSignPage" />
		</uni-list>
		<button style="margin-top: 10px;" :disabled="realPass"
			@click="realName">{{realPass?'已 实 名 认 证':'实 名 认 证'}}</button>
	</view>
</template>

<script>
	import {
		idCardPass,
		namePass,
		phonePass
	} from '@/utils/PrivacyPass.js'
	import {
		type
	} from 'os';
	export default {
		data() {
			return {
				userInfo: {},
				userAUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
				flag: false,
				token: '',
				remark: {},
				type: 0,
				realPass: false,
				showPrivacy: true, // 隐私展示
			}
		},
		onShow() {
			let obj = uni.getStorageSync('userInfo')
			this.userInfo = typeof obj == 'object' ? obj : {}
			this.type = uni.getStorageSync('type') || 0
			this.getToken()
			this.AuthRealName()
			uni.login({
				provider: 'weixin',
				success: (res) => {
					console.log(res);
				}
			})
		},
		// onLoad() {
		// 	uni.navigateTo({
		// 		url: '/pages/webView/facePass'
		// 	})
		// },
		computed: {
			setSign() {
				console.log(this.userInfo);
				console.log(typeof this.userInfo.sign);
				return typeof this.userInfo.sign == 'string' ? '已设置' : '未设置'
			}
		},
		methods: {

			toSignPage() {
				uni.navigateTo({
					url: '/pages/SignPage/SignPage'
				})
			},
			// 提示
			showToast() {
				let title = ''
				if (this.realPass) title = '您已完成实名认证'
				else title = '您的验证方式为人脸识别,无需进行输入'
				uni.showToast({
					icon: 'none',
					title
				})
			},
			// 隐私加密
			idCardPass(text) {
				if (this.showPrivacy) return idCardPass(text)
				else return text || ''
			},
			namePass(text) {
				if (this.showPrivacy) return namePass(text)
				else return text || ''
			},
			phonePass(text) {
				if (this.showPrivacy) return phonePass(text)
				else return text || ''
			},
			// 用户头像
			onChooseAvatar(e) {
				if (!this.realPass) return uni.showToast({
					icon: 'none',
					title: '请完成信息验证后再进行头像上传'
				})
				uni.chooseImage({
					count: 1,
					sourceType: ['camera'],
					success: (rst) => {
						uni.navigateTo({
							url: '/pages/UpdateInfo/camera?path=' + encodeURIComponent(rst
								.tempFilePaths[0])
						})
					}
				});
			},
			realName() {
				if (this.type == 1) {
					this.facePass()
					return
				} else {
					// 信息不完善
					if (!this.userInfo.name || !this.userInfo.idCard) return uni.showToast({
						title: '请完善信息',
						icon: 'none'
					})
					// 非扫码进入
					// if (!uni.getStorageSync('scanCode')) return uni.showToast({
					// 	title: '请扫描二维码,获取公司验证方式',
					// 	icon: 'none'
					// })
					// 百度验证已通过 直接调用保存
					if (this.flag) this.certification()
					if (this.type == 0) this.baiduPass()
					else if (this.type == 2) this.threeEle()
					else uni.showToast({
						icon: 'none',
						title: '请扫描二维码确认企业认证方式'
					})
				}
			},
			// 人脸识别
			facePass() {
				uni.showLoading()
				uni.request({
					url: 'https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=' +
						this.token,
					method: 'POST',
					header: {
						'Content-Type': 'application/json'
					},
					data: {
						plan_id: '16384'
					}
				}).then(result => {
					uni.hideLoading()
					let [err, res] = result;
					if (err !== null) flag = true
					console.log(res);
					if (res.statusCode == 200) {
						let token = res.data.result.verify_token
						let successUrl = encodeURIComponent('https://hzq.yoojet.com/#/pass/facePassLoading')
						let url =
							`token=${token}&successUrl=${successUrl}&failedUrl=https://hzq.yoojet.com/#/pass/facePassError`
						let newUrl = encodeURIComponent(url)
						uni.navigateTo({
							url: '/pages/webView/facePass?url=' + newUrl + '&at=' + this.token + '&vt=' +
								token
						})
					}
				})
			},
			// 三要素
			threeEle() {
				uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/auth3',
					method: 'POST',
					data: {
						"phone": this.userInfo.phone,
						"name": this.userInfo.name,
						"idNumber": this.userInfo.idCard,
						"state": true
					}
				}).then(result => {
					let [err, res] = result;
					if (err !== null) flag = true
					if (res.data.code == 200) {
						let resp = JSON.parse(res.data.data)
						if (resp.code == 0) {
							this.remark = resp
							this.flag = true
							this.certification()
						} else {
							uni.showToast({
								title: '验证失败,请检查身份信息是否正确',
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '验证失败,请尝试重新提交',
							icon: 'none'
						})
					}
				})
			},
			// 百度校验身份信息
			baiduPass() {
				uni.showLoading({
					title: '校验中,请稍等'
				})
				uni.request({
					url: 'https://aip.baidubce.com/rest/2.0/face/v3/person/idmatch?access_token=' + this
						.token,
					method: 'POST',
					data: {
						"id_card_number": this.userInfo.idCard,
						"name": this.userInfo.name
					},
					header: {
						'Content-Type': 'application/json' //配置请求头
					},
				}).then(res => {
					uni.hideLoading()
					if (res.length >= 2) {
						let result = res[1]
						if (result.data.error_code == 0) {
							this.flag = true
							this.certification()
						} else {
							uni.showToast({
								title: '校验失败,身份证号与姓名不匹配',
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: '校验失败,身份证号与姓名不匹配',
							icon: 'none'
						})
					}
				})
			},
			// 保存认证记录
			certification() {
				uni.request({
					url: this.$veriUrl + '/auth/VeriRecord',
					method: 'POST',
					data: {
						"phone": this.userInfo.phone,
						"name": this.userInfo.name,
						"idNumber": this.userInfo.idCard,
						"state": true,
						VeriType: this.type,
						remark: JSON.stringify(this.remark)
					}
				}).then(result => {
					let [err, res] = result;
					if (err !== null) flag = true
					if (res.data.code == 200) {
						uni.showToast({
							title: '验证成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					} else {
						uni.showToast({
							title: '验证失败,请尝试重新提交',
							icon: 'none'
						})
					}
				})
			},
			// 获取TOKEN
			async getToken() {
				await uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/token?phone=' + this.userInfo.phone,
					method: 'GET'
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						this.token = JSON.parse(res.data.data).access_token
					}
				})
			},
			// 验证是否实名认证
			async AuthRealName() {
				await uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/state?Value=' + this.userInfo.phone,
					method: 'GET'
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						this.realPass = true
						this.userInfo = {
							...res.data.data,
							name: res.data.data.name || this.userInfo.name,
							phone: res.data.data.phone || this.userInfo.phone,
							idCard: res.data.data.idNumber || this.userInfo.idCard,
							avatarUrl: res.data.data.avatarUrl || this.userInfo.avatarUrl,
							smscode: res.data.data.smscode || this.userInfo.smscode,
						}
						if (this.userInfo.name) this.getBG(this.userInfo.name)
						else uni.removeStorageSync('nameBG')
					} else {
						this.realPass = false
					}
					uni.setStorageSync('userInfo', this.userInfo)
				})
			},
			getBG(name) {
				this.$http('post', '/Base_Manage/Upload/GetNameSign?Name=' + name).then(res => {
					if (res.Success) uni.setStorageSync('nameBG', res.Data)
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.avatar-wrapper {
		margin: 10px 0 0 10px;
		border: none;
		background-color: transparent;
		width: 100px;
		height: 100px;
		padding: 0;
		border-radius: 5px;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.flex {
		display: flex;
		justify-content: space-between;
		align-items: baseline;
	}
</style>